package cn.chenc.framework.demo.config;


import cn.chenc.framework.dynamic.datasource.DynamicDataSource;

import cn.chenc.framework.dynamic.datasource.config.properties.DruidProperties;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.validation.annotation.Validated;

import javax.sql.DataSource;
import java.util.HashMap;
import java.util.Map;

/**
 * 　@description: druid 多数据源配置
 * 　@author secret
 * 　@date 2021/7/30 23:40
 *
 */
@Configuration
public class DruidConfig {


    @Bean(name = "masterDruidProperties")
    @ConfigurationProperties(prefix="spring.datasource.druid.master")
    public DruidProperties masterDruidProperties(){
        return new DruidProperties();
    }


    @Bean(name = "slaveDruidProperties")
    @ConfigurationProperties("spring.datasource.druid.slave")
    public DruidProperties slaveDruidProperties(){
        return new DruidProperties();
    }

    @Bean
    public DataSource masterDataSource(@Qualifier("masterDruidProperties") DruidProperties masterDruidProperties) {
        DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
        return masterDruidProperties.dataSource(dataSource);
    }

    @Bean
    public DataSource slaveDataSource(@Qualifier("slaveDruidProperties") DruidProperties slaveDruidProperties) {
        DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
        return slaveDruidProperties.dataSource(dataSource);
    }

    @Bean(name = "dynamicDataSource")
    @Primary
    public DynamicDataSource dataSource(Map<String, DataSource> dataSources) {
        DynamicDataSource dynamicDataSource = new DynamicDataSource();
        Map<Object, Object> targetDataSources = new HashMap<>();
        targetDataSources.putAll(dataSources);
        dynamicDataSource.setTargetDataSources(targetDataSources);
        dynamicDataSource.setDefaultTargetDataSource(dataSources.get("masterDataSource"));
        dynamicDataSource.afterPropertiesSet();
        // 遍历存入list中
        for (Map.Entry<String, DataSource> entry : dataSources.entrySet()) {
            DynamicDataSource.saveDataSourceTypeName(entry.getKey());
        }
        return dynamicDataSource;
    }

    /**
     * 向spring容器暴露组合数据源JdbcTemplate
     * @param dataSource
     * @return
     * @author secret
     */
    @Bean(name = "multipleDataJdbcTemplate")
    public JdbcTemplate primaryJdbcTemplate(@Qualifier("dynamicDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }



}
